﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace 期末作业
{
    public partial class Form2 : Form
    {

        Dictionary<string, StuInfo> StuDic = new Dictionary<string, StuInfo>();
        private void FillGrid(Dictionary<string, StuInfo> dic)
        {
            if (dataGridViewStuInfo.ColumnCount == 0)
            {
                DataGridViewTextBoxColumn col_StuID = new DataGridViewTextBoxColumn();
                col_StuID.HeaderText = "学生学号";
                col_StuID.DataPropertyName = "ID";
                col_StuID.Name = "StuID";

                DataGridViewTextBoxColumn col_StuName = new DataGridViewTextBoxColumn();
                col_StuName.HeaderText = "姓名";
                col_StuName.DataPropertyName = "Name";
                col_StuName.Name = "StuName";

                DataGridViewTextBoxColumn col_StuSex = new DataGridViewTextBoxColumn();
                col_StuSex.HeaderText = "性别";
                col_StuSex.DataPropertyName = "Sex";
                col_StuSex.Name = "StuSex";

                DataGridViewTextBoxColumn col_StuAge = new DataGridViewTextBoxColumn();
                col_StuAge.HeaderText = "年龄";
                col_StuAge.DataPropertyName = "Age";
                col_StuAge.Name = "StuAge";

                DataGridViewTextBoxColumn col_StuClassID = new DataGridViewTextBoxColumn();
                col_StuClassID.HeaderText = "班级编号";
                col_StuClassID.DataPropertyName = "ClassID";
                col_StuClassID.Name = "StuClassID";

                dataGridViewStuInfo.Columns.Add(col_StuID);
                dataGridViewStuInfo.Columns.Add(col_StuName);
                dataGridViewStuInfo.Columns.Add(col_StuSex);
                dataGridViewStuInfo.Columns.Add(col_StuAge);
                dataGridViewStuInfo.Columns.Add(col_StuClassID);
            }
            BindingSource bs = new BindingSource();
            bs.DataSource = dic.Values;
            dataGridViewStuInfo.DataSource = bs;
        }
        public Form2()
        {
            InitializeComponent();
            this.Text = "学生信息管理系统";

            StuInfo wang = new StuInfo("20190620", "黄蕨", "2", "19", "1001");
            StuInfo li = new StuInfo("20190912", "王浩", "1", "18", "1002");

            StuDic.Add(li.ID, li);
            StuDic.Add(wang.ID, wang);
            FillGrid(StuDic);
        }

        private void Form2_Load(object sender, EventArgs e)
        {

        }

       

        private void ButtonQuery_Click_1(object sender, EventArgs e)
        {
            if (textBoxQuery.Text == "")
            {
                FillGrid(StuDic);
                return;
            }
            if (!StuDic.ContainsKey(textBoxQuery.Text))
            {
                MessageBox.Show("请查询存在的学号！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            StuInfo s = StuDic[textBoxQuery.Text];
            Dictionary<string, StuInfo> dic = new Dictionary<string, StuInfo>();
            dic.Add(s.ID, s);
            FillGrid(dic);
        }

        private void buttonDele_Click_1(object sender, EventArgs e)
        {
            if (!StuDic.ContainsKey(textBoxQuery.Text))
            {
                MessageBox.Show("请先查询存在的学号再点击删除！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            StuDic.Remove(textBoxQuery.Text);
            FillGrid(StuDic);
        }

        private void buttonEdit_Click_1(object sender, EventArgs e)
        {
            if (!StuDic.ContainsKey(textBoxQuery.Text))
            {
                MessageBox.Show("请先查询存在的学号再点击修改！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            textBoxStuID.Enabled = false;
            StuInfo s = StuDic[textBoxQuery.Text];
            textBoxStuID.Text = s.ID;
            textBoxName.Text = s.Name;
            comboBoxSex.Text = s.Sex;
            textBoxAge.Text = s.Age;
            textBoxClassID.Text = s.ClassID;

        }

        private void buttonAdd_Click_1(object sender, EventArgs e)
        {
            textBoxStuID.Text = "";
            textBoxName.Text = "";
            comboBoxSex.Text = "";
            textBoxAge.Text = "";
            textBoxClassID.Text = "";
            textBoxStuID.Enabled = true;
        }

        private void buttonOK_Click_1(object sender, EventArgs e)
        {
            if (textBoxStuID.Enabled)
            {
                if (StuDic.ContainsKey(textBoxStuID.Text))
                {
                    MessageBox.Show("学号已存在！", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                if (textBoxStuID.Text == "" || textBoxName.Text == "" || textBoxName.Text == "" || comboBoxSex.Text == "" || textBoxAge.Text == "" || textBoxClassID.Text == "")
                {
                    MessageBox.Show("请将信息填写完整！", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                StuInfo s = new StuInfo(textBoxStuID.Text, textBoxName.Text, comboBoxSex.Text, textBoxAge.Text, textBoxClassID.Text);
                StuDic.Add(s.ID, s);
            }
            else
            {
                if (textBoxName.Text == "" || textBoxName.Text == "" || comboBoxSex.Text == "" || textBoxAge.Text == "" || textBoxClassID.Text == "")
                {
                    MessageBox.Show("请将信息填写完整！", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                StuDic.Remove(textBoxStuID.Text);
                StuInfo s = new StuInfo(textBoxStuID.Text, textBoxName.Text, comboBoxSex.Text, textBoxAge.Text, textBoxClassID.Text);
                StuDic.Add(s.ID, s);
            }
            FillGrid(StuDic);
            textBoxStuID.Text = "";
            textBoxName.Text = "";
            comboBoxSex.Text = "";
            textBoxAge.Text = "";
            textBoxClassID.Text = "";

        }

        private void buttonCel_Click_1(object sender, EventArgs e)
        {
            textBoxStuID.Text = "";
            textBoxName.Text = "";
            comboBoxSex.Text = "";
            textBoxAge.Text = "";
            textBoxClassID.Text = "";
        }

 


    

    }
}
